<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Cart;
use App\Models\MallFullReduction;
use App\Models\MallProductStock;
use App\Models\Product;
use App\Services\FullReductionService;
use Illuminate\Http\Request;

class ProductController extends BaseController
{
    public $model = Product::class;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $search = ['title' => '', 'category_id' => 0, 'on_status' => 1, 'parent_category_id' => 0
            , 'start_at' => null, 'end_at' => null, 'is_new' => -1];
        $input = $request->input();

        $producer_id = $input['producer_id'] ?? 0;
        $is_full_reduction = $input['is_full_reduction'] ?? -1;

        $order_by_key = $request->input('order_by_key', 'score');
        $order_by_value = $request->input('order_by_value', 'desc');
        $field = ["id", 'title', 'image_url', 'amount', 'vip_amount','type', 'video_url'];
        $list = Product::apply($search, $input)->orderBy($order_by_key, $order_by_value)
            ->when($producer_id, function ($q) use($producer_id){
                $q->where('producer_id', $producer_id);
            })
            ->when($is_full_reduction != -1, function ($q) use($is_full_reduction){
                $q->where('is_full_reduction', $is_full_reduction);
            })
            ->select($field)
            ->orderBy('id', 'desc')
            ->paginate(\request('per_page', 20));


        $arr = [];
        if ($producer_id && $is_full_reduction) {
            $user = $this->getUser();
            $amount = MallFullReduction::where('merchant_id', $producer_id)->value('amount') ?? 0;
            $left_amount = FullReductionService::handle($producer_id, $user);
            $arr['full_total_amount'] = $amount;
            $arr['left_amount'] = $left_amount;
        }

        return $this->paginate($list, $arr);
    }



    public function info(Request $request)
    {
        $id = \request("id");
        if (!$id) {
            return $this->failed("参数ID不存在");
        }

        $info = Product::with(['full_reduction'])->find($id);

        if (!$info)
            return $this->failed("商品不存在");
        $info->is_buy = false;

        if ($info->on_status != 1)
            return $this->failed("商品未上架");

        $info = $info->toArray();
        if ($info['is_specs']) {
            $specs_detail_info = $info['specs_detail_info'];
            foreach ($specs_detail_info as $key => &$item) {
                $stock = MallProductStock::where('product_id', $info['id'])->where('key', $key)->value('stock') ?: 0;
                $item['stock'] = $stock;
//                $item['stock'] = $item['stock'] > 0 ? $item['stock'] : 0;
            }
            $info['specs_detail_info'] = $specs_detail_info;
            $info['specs_detail']['info'] = array_values($specs_detail_info);
        }

//        if ($info['is_specs']) {
//            $temp = $info['specs_detail']['info'];
//            $arr_info = $temp;
//            $arr_key = [];
//            foreach ($arr_info as $key => $item) {
//                $val= $item;
//                $key = MallOrderService::specsKey($val);
//                array_push($arr_key, $key);
//            }
//            $arr_sale_num = MallOrderService::getSaleNum($id, $arr_key);
//
//            foreach ($arr_info as $key => &$item) {
//                $val= $item;
//                $key = MallOrderService::specsKey($val);
//                $item['stock'] = $item['stock'] - ($arr_sale_num[$key] ?? 0);
//            }
//
//            $info['specs_detail']['info'] = $arr_info;
//        } else {
//            $sale_num = MallOrderService::getSaleNum($id);
//            $info['stock'] = $info['stock'] - ($sale_num ?: 0);
//        }
        $user = $this->getUser();
        $info['is_vip'] = $user->is_vip ?? 0;
        $info['cart_total'] = $user ? Cart::where('user_id', $user->id)->where('is_direct', 0)->count() : 0;
        return $this->success($info);
    }


}

